<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/1/20
 * Time: 13:57
 */

namespace app\api\model;

use think\Db;
use think\Model;
use app\Config;
use PDO;

class Resource extends Model
{
    public $pdo = '';
    public $dbName = '';

    public function __construct()
    {
        $dbms = $this->getConfig('type');     //数据库类型
        $host = $this->getConfig('hostname'); //数据库主机名
        $this->dbName = $this->getConfig('database');    //使用的数据库
        $this->pdo = new PDO("$dbms:host=$host;dbname=$this->dbName", $this->getConfig('username'), $this->getConfig('password'));
    }

    /**
     * 检测数据表是否存在
     * @param string $table
     * @return bool
     */
    public function checkTableExist($table = '')
    {
        $tableName = $table ? $table : self::getTable();
        $sql = sprintf("SELECT COUNT(1) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`='%s' AND `TABLE_NAME` = '%s'", $this->dbName, $this->getConfig('prefix') . $tableName);
        $result = $this->pdo->query($sql);
        if ($result === false) {//表不存在
            return false;
        }
        if (!$result->fetchColumn(0)) {
            return false;
        }
        return true;
    }

    /**
     * 复制表结构
     * @param string $table
     * @param string $newTable
     * @param string $company_id
     * @return bool
     */
    public function copyTable($table = '', $newTable = '', $company_id = '')
    {
        $table = $this->getConfig('prefix') . $table;
        $newTable = $this->getConfig('prefix') . $newTable;
        $sql = sprintf("create table %s like %s", $newTable, $table);
        $result = $this->pdo->query($sql);
        if ($result === false) {
            return false;
        }
        $sql = sprintf("alter table %s comment '%s'", $newTable, '公司-' . $company_id . "_" . $table . '分表');
        $this->pdo->query($sql);
        return true;
    }

    /**
     * id获取资源信息
     * @param string $id
     * @param string $company_id
     * @param string $module
     * @param int $status
     * @param string $user_id
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getResourceByIds($id = '', $company_id = '', $module = '', $status = 1, $user_id = '')
    {
        //资源表
        $resourceTbale = "resource" . "_" . $company_id;
        if ($this->checkTableExist($resourceTbale) == false) {
            return [];
        }

        $where['id'] = ['in', $id];
        !empty($module) && $where['module'] = $module;
        !empty($user_id) && $where['user_id'] = $user_id;
        $where['status'] = $status;
        return DB::name($resourceTbale)->where($where)->select();
    }

    /**
     * 插入资源数据
     * @param array $data
     * @param string $user_id
     * @param int $company_id
     * @param int $status
     * @return bool|string
     */
    public function insertResource($data = [], $user_id = '', $company_id = 0, $status = 1)
    {
        if (!isset($data['path']) || empty($data['path'])) {
            return false;
        }
        $resourceTbale = "resource";
        $newResourceTbale = $resourceTbale . "_" . $company_id;
        if ($this->checkTableExist($newResourceTbale) == false) {
            $this->copyTable($resourceTbale, $newResourceTbale, $company_id);
        }
        $insertData['module'] = isset($data['module']) ? $data['module'] : 'product';
        $insertData['name'] = isset($data['name']) ? $data['name'] : '';
        $insertData['path'] = $data['path'];
        $insertData['size'] = isset($data['size']) ? $data['size'] : 0;
        $insertData['postfix'] = isset($data['postfix']) ? $data['postfix'] : '';
        $insertData['original'] = isset($data['original']) ? $data['original'] : $insertData['name'];
        $insertData['timestamp'] = time();
        $insertData['status'] = $status;
        $insertData['user_id'] = $user_id;
        DB::name($newResourceTbale)->insert($insertData);
        return Db::name($newResourceTbale)->getLastInsID();
    }

}